<template>
    <rx-dialog @handOk="okFun" @cancel="cancel" :showcancel="true" :showok="true">
        <rx-layout>
            <div slot="center">
<!--                <div style="padding: 20px;">表名：{{tableName}}</div>-->
                <a-table :columns="columns" :data-source="tableData" :pagination="false" id="chkTable"
                         :scroll="{y:420}"  ref="checkTable" :rowSelection="rowSelection" rowKey="tableName" >
                    <template slot="operation" slot-scope="text, record, index">
                        <a>{{i18n('appImport.selectData','选择数据')}}</a>
                    </template>
                    <template slot="cover" slot-scope="text, record, index">
                        <span>{{index + 1}}/{{index + 1}}</span>
                    </template>
                </a-table>
                <div style="margin-top: 20px; padding-left: 30px;">
                    <a-checkbox :defaultChecked="selectChecked" @click="selectAll($event)" v-if="refreshCheck">
                        <span :title="i18n('appImport.ifSelected','选中则意味着将覆盖ID相同的记录')">{{i18n('appImport.selectAll','全选/全不选')}}</span>
                    </a-checkbox>
                </div>

            </div>
        </rx-layout>
    </rx-dialog>
</template>
<script>
import {BaseForm, RxDialog,  Util, RxMonacoEditor,RxSelect} from "@lowcode/jpaas-common-lib";
import SysAppApi from "@/api/system/core/sysApp";
export default {
    name: 'app-import',
    mixins: [BaseForm],
    components: {
        RxDialog,RxSelect,
        RxMonacoEditor
    },
    props: {
        layerid: String,
        lydata: Object,
        destroy: Function,
        override: {
            type: Array
        },
        checkData: {
            type: Array
        },
        checkResourceData: {
            type: Array
        },
        callback: {
            type: Function
        }
    },
    data() {
        return {
            //检查返回信息
            selectedKeys:[],
            selectedRows:[],
            columns: [
                {
                    title: this.i18n('form.sn','序号'),
                    dataIndex: 'index',
                    key: 'index',
                    align: 'left',
                    width: 50,
                    customRender: (text, record, index) => `${index + 1}`,
                },
                {
                    title: this.i18n('appImport.table','表名'),
                    dataIndex: 'tableDesc',
                    key: 'tableDesc',
                    align: 'left'
                },
                {
                    title: this.i18n('appImport.tableName','物理表名'),
                    dataIndex: 'tableName',
                    key: 'tableName',
                    align: 'left'
                },
                {
                    title: this.i18n('appImport.type','类型'),
                    dataIndex: 'type',
                    key: 'type',
                    align: 'left'
                },
            ],
            tableData: [],
            selectChecked: false,
            refreshCheck: true
        }
    },
    created() {
        this.setSysTableData();
    },
    computed: {
        rowSelection() {
            return {
                //columnTitle:"ID相同的记录则覆盖",
                columnTitle:{
                    tag:'span',
                    text:this.i18n('appImport.check','勾选'),
                    data:{attrs:{title:this.i18n('appImport.ifSelected','选中则意味着将覆盖ID相同的记录')}}
                },
                columnWidth:80,
                onChange: (selectedRowKeys, selectedRows) => {
                    this.selectedKeys=selectedRowKeys;
                },
                selectedRowKeys:this.selectedKeys
            };
        },
        errMsg(){
            return this.getResult("errMsg");
        },
        alertSql(){
            //if(this.checkInfo)
            return this.getResult("alertSql");

        }
    },
    methods: {
        i18n(name,text,key){
            return this.$ti18(name,text,"SysApp",key);
        },
        // 设置系统表
        setSysTableData() {
            let length = this.override.length;
            let ids = [];
            let type = null;
            for (let i = 0; i < length; i++) {
                let data = this.override[i];
                if (data.type = 'all' || (data.type = 'part' && data.ids.length >0)) {
                    this.tableData.push(
                        {
                            tableDesc: data.tableDesc,
                            tableName: data.tableName,
                            type: this.i18n('appImport.systemTable','系统表')
                        }
                    )
                    if (data.tableName == 'form_bo_entity') {
                        ids = data.ids;
                        type = data.type;
                    }
                }

            }
            this.setBusTableData(type, ids);;
        },
        // 设置业务表
        setBusTableData(type, ids) {
            let length = this.checkData.length;
            for (let i = 0; i < length; i++) {
                let data = this.checkData[i];
                if (data.tableName == 'form_bo_entity') {
                    let repeatDataList = data.repeatDataList;
                    repeatDataList.forEach(repeat => {
                        let obj = {
                            tableDesc: repeat.NAME_,
                            tableName: repeat.TABLE_NAME_,
                            type: this.i18n('appImport.businessTable','业务表'),
                            dataSource: repeat.DS_ALIAS_
                        }
                        if (type == 'all') {
                            this.tableData.push(obj)
                        } else {
                            if (ids.includes(repeat.ID_)) {
                                this.tableData.push(obj);
                            }
                        }
                    })
                    break;
                }
            }
        },
        selectAll(event){
            let chk=true;
            if(event){
                chk=event.target.checked;
            }
            let rowKeys=[];
            if (chk){
                let data;
                for(data of this.tableData) {
                    rowKeys.push(data["tableName"]);
                }
            }
            this.selectedKeys=rowKeys;
        },
        getTableData() {
            let data = [];
            let length = this.tableData.length;
            this.selectedKeys.forEach(id => {
                for (let i = 0; i < length; i++) {
                    let obj = this.tableData[i];
                    if (id == obj.tableName) {
                        data.push(obj)
                        break;
                    }
                }
            })
            let returnData = [];
            console.log(this.checkResourceData)
            let checkDataLength = this.checkResourceData.length;

            data.forEach(object => {
                let tableName = object.tableName;
                let dataSource = object.dataSource;
                if (dataSource) {
                    if (returnData.length == 0) {
                        returnData.push({
                            alias: 'form',
                            tables: [object]
                        })
                    } else {
                        returnData.forEach(ret => {
                            let alias = ret.alias;
                            if (alias == 'form') {
                                if (ret.tables) {
                                    ret.tables.push(object);
                                } else {
                                    ret.tables = [object];
                                }
                            }
                        })
                    }
                } else {
                    for (let i = 0; i < checkDataLength; i++) {
                        let obj = this.checkResourceData[i];
                        let alias = obj.alias;
                        let info = obj.checkInfoList;
                        let infoLength = info.length;
                        for (let j = 0; j < infoLength; j++) {
                            let table = info[j].tableName;
                            if (table == tableName) {
                                if (returnData.length == 0) {
                                    returnData.push({
                                        alias: alias,
                                        tables: [object]
                                    })
                                } else {
                                    let flag = true;
                                    returnData.forEach(ret => {
                                        let alias1 = ret.alias;
                                        if (alias1 == alias) {
                                            ret.tables.push(object);
                                            flag = false;
                                        }
                                    })
                                    if (flag) {
                                        returnData.push({
                                            alias: alias,
                                            tables: [object]
                                        })
                                    }
                                }
                                break;
                            }
                        }

                    }
                }

            })
            return returnData;
        },
        okFun() {
            let returnData = this.getTableData();
            console.log(returnData)
            Util.closeWindow(this, "ok", returnData);
        },
        cancel() {
            Util.closeWindow(this, "cancel", null);
        }
    }
}
</script>
<style scoped>
.title-div {
    padding: 10px 0;
    font-weight: bold;
    color: navy;
}

* >>> .ant-table-thead > tr > th {
    text-align: center;
}

* >>> .ql-snow .ql-picker-label::before {
    vertical-align: top;
}

* >>> .ql-snow .ql-color-picker .ql-picker-label svg,
* >>> .ql-snow .ql-icon-picker .ql-picker-label svg {
    vertical-align: top;
}

.avatar-uploader > .ant-upload {
    width: 128px;
    height: 128px;
}

.ant-upload-select-picture-card i {
    font-size: 32px;
    color: #999;
}
.ant-upload-select-picture-card .ant-upload-text {
    margin-top: 8px;
    color: #666;
}
</style>